{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-46dfc9ee00c9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     47\u001b[0m     \u001b[1;31m#reload(sys)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     48\u001b[0m     \u001b[1;31m#sys.setdefaultencoding('gb18030')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 49\u001b[1;33m     \u001b[0mwrite_excel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-24-46dfc9ee00c9>\u001b[0m in \u001b[0;36mwrite_excel\u001b[1;34m()\u001b[0m\n\u001b[0;32m     27\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mwrite_excel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     28\u001b[0m     \u001b[0mf\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mxlwt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mWorkbook\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 29\u001b[1;33m     \u001b[0msheet1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_sheet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'学生'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcell_overwrite_ok\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     30\u001b[0m     \u001b[0mrow0\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"姓名\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"年龄\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"出生日期\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"爱好\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m     \u001b[0mcolum0\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"张三\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"李四\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"恋习Python\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"小明\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"小红\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"无名\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\Program\\Anaconda2\\lib\\site-packages\\xlwt\\Workbook.py\u001b[0m in \u001b[0;36madd_sheet\u001b[1;34m(self, sheetname, cell_overwrite_ok)\u001b[0m\n\u001b[0;32m    364\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0mWorksheet\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mWorksheet\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    365\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msheetname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0municode_type\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 366\u001b[1;33m             \u001b[0msheetname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msheetname\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    367\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mUtils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalid_sheet_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msheetname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    368\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"invalid worksheet name %r\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0msheetname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)"
     ]
    }
   ],
   "source": [
    "#! /usr/bin/python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "#python 读写 excel\n",
    "import xlwt\n",
    "import sys\n",
    "\n",
    "#reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法，我们需要重新载入 \n",
    "#sys.setdefaultencoding('utf-8')\n",
    "defaultencoding = 'utf-8'\n",
    "if sys.getdefaultencoding() != defaultencoding:\n",
    "    reload(sys)\n",
    "    sys.setdefaultencoding(defaultencoding)\n",
    "\n",
    "#设置表格样式\n",
    "def set_style(name,height,bold=False):\n",
    "    style=xlwt.XFStyle()\n",
    "    font=xlwt.Font()\n",
    "    font.name=name\n",
    "    font.bold=bold\n",
    "    font.color_index=4\n",
    "    font.height=height\n",
    "    style.font=font\n",
    "    return style\n",
    "\n",
    "#写Excel\n",
    "def write_excel():\n",
    "    f=xlwt.Workbook()\n",
    "    sheet1=f.add_sheet('学生',cell_overwrite_ok=True)\n",
    "    row0=[\"姓名\",\"年龄\",\"出生日期\",\"爱好\"]\n",
    "    colum0=[\"张三\",\"李四\",\"恋习Python\",\"小明\",\"小红\",\"无名\"]\n",
    "    #写第一行\n",
    "    for i in range(0,len(row0)):\n",
    "        sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))\n",
    "    #写第一列\n",
    "    for i in range(0,len(colum0)):\n",
    "        sheet1.write(i+1,0,colum0[i],set_style('Times New Roman',220,True))\n",
    "        \n",
    "    sheet1.write(1,3,'2006/12/12')\n",
    "    sheet1.write_merge(6,6,1,3,'未知') #合并行单元格\n",
    "    sheet1.write_merge(1,2,3,3,'打游戏') #合并列单元格\n",
    "    sheet1.write_merge(4,5,3,3,'打篮球')\n",
    "    \n",
    "    f.save('test.xls')\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    #reload(sys)\n",
    "    #sys.setdefaultencoding('gb18030')\n",
    "    write_excel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "#! /usr/bin/python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "#python 读写 excel\n",
    "import xlwt\n",
    "import sys\n",
    "\n",
    "#reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法，我们需要重新载入 \n",
    "#sys.setdefaultencoding('utf-8')\n",
    "defaultencoding = 'utf-8'\n",
    "if sys.getdefaultencoding() != defaultencoding:\n",
    "    reload(sys)\n",
    "    sys.setdefaultencoding(defaultencoding)\n",
    "\n",
    "#设置表格样式\n",
    "def set_style(name,height,bold=False):\n",
    "    style=xlwt.XFStyle()\n",
    "    font=xlwt.Font()\n",
    "    font.name=name\n",
    "    font.bold=bold\n",
    "    font.color_index=4\n",
    "    font.height=height\n",
    "    style.font=font\n",
    "    return style\n",
    "\n",
    "#写Excel\n",
    "def write_excel():\n",
    "    f=xlwt.Workbook()\n",
    "    sheet1=f.add_sheet('student',cell_overwrite_ok=True)\n",
    "    row0=[\"Name\",\"Age\",\"Birthday\",\"Habbit\"]\n",
    "    colum0=[\"Zhang San\",\"Li Si\",\"Practice Python\",\"Xiao Min\",\"Xiao Hong\",\"Unknown\"]\n",
    "    #写第一行\n",
    "    for i in range(0,len(row0)):\n",
    "        sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))\n",
    "    #写第一列\n",
    "    for i in range(0,len(colum0)):\n",
    "        sheet1.write(i+1,0,colum0[i],set_style('Times New Roman',220,True))\n",
    "        \n",
    "    sheet1.write(1,3,'2006/12/12')\n",
    "    sheet1.write_merge(6,6,1,3,'Unkown') #合并行单元格\n",
    "    sheet1.write_merge(1,2,3,3,'Play Video Games') #合并列单元格\n",
    "    sheet1.write_merge(4,5,3,3,'Play Basket Balls')\n",
    "    \n",
    "    f.save('test.xls')\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    #reload(sys)\n",
    "    #sys.setdefaultencoding('gb18030')\n",
    "    write_excel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
